<?php
namespace App\Http\Controllers\ApiXcx;

use App\Service\ApiXcx\CartService;
use Illuminate\Http\Request;

class CartController extends BaseController
{
    //购物车

    public function __construct()
    {
        $this->db = new CartService();
    }

    //列表
    public function getList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $res = $this->db->getList($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //添加
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $sku_id = $request->post('sku_id', 0);
        $pas['shop_id'] = $request->post('shop_id', 0);
        $pas['num'] = $request->post('num', 1);
        $pas['delivery'] = $request->post('delivery', 1);
        if (!$sku_id) {
            return $this->toJson(-2, '商品SKUID必须的');
        }
        $res = $this->db->add($uid, $sku_id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //加减
    public function reduce(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $cart_id = $request->post('cart_id', 0);
        $pas['num'] = $request->post('num', 0);
        $pas['sku_id'] = $request->post('sku_id', 0);
        if (!$cart_id) {
            return $this->toJson(-2, '购物车ID必须的');
        }
        if (!$pas['num']) {
            return $this->toJson(-2, '数量必须的');
        }
        if (!$pas['sku_id']) {
            return $this->toJson(-2, '规格必须的');
        }
        $res = $this->db->reduce($uid, $cart_id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

//    //批量删除
//    public function batchDel(Request $request)
//    {
//        $token = $request->post('token', '');
//        $uid = $this->db->addApiCount($token);
//        $gids = $request->post('gids', '');
//        if (!$gids) {
//            return $this->toJson(-2, '购物车ID必须的');
//        }
//        $gids = explode(',', $gids);
//        $res = $this->db->batchDel($uid, $gids);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson();
//    }

    //批量删除
    public function batchDel(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return array('code' => -2, 'msg' => '用户不存在');
        }
        $cart_ids = $request->post('cart_ids', '');
        if (!$cart_ids) {
            return $this->toJson(-2, '购物车ID必须的');
        }
        $cart_ids = explode(',', $cart_ids);
        $res = $this->db->batchDel($uid, $cart_ids);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //转换
    public function zhuanhuan(Request $request)
    {
        $num = $request->post('num', 0);
        $price = $request->post('price', 0);
        if (!$num) {
            return $this->toJson(-2, '数量必须的');
        }
        if (!$price) {
            return $this->toJson(-2, '价格必须的');
        }
        $price = ceil($price * 100);
        $amount = $price * $num / 100;
        $res = [
            'amount' => $amount,
        ];
        return $this->toJson(0, '', $res);
    }
}